﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine.Graphics.Shader;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using UnityEngine.Components;
using UnityEngine.Gameplay.Match;

namespace UnityEngine.Graphics
{
    public class Portrait : IDisposable
    {
        private Camera _Camera;
        private RenderTarget2D _RenderTareget;
        private ModelRenderer _ModelRenderer;

        public TeamRelationType TeamType
        {
            get;
            set;
        }

        public RenderTarget2D RenderTarget
        {
            get
            {
                return _RenderTareget;
            }
        }

        public ModelRenderer ModelRender
        {
            set
            {
                _ModelRenderer = value;
            }
        }

        ~Portrait()
        {
            this.Dispose(false);
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_RenderTareget != null)
                    _RenderTareget.Dispose();
            }
        }

        public void UpdatePortrait()
        {
            EffectManager.Instance.GraphicsDevice.SetRenderTarget(_RenderTareget);
            EffectManager.Instance.GraphicsDevice.Clear(Color.Transparent);
            if (_ModelRenderer != null)
            {
                Vector3 v = _ModelRenderer.BoundingBox.Max - _ModelRenderer.BoundingBox.Min;
                v = Vector3.Transform(v, _Rotation);
                float max = v.Z;
                if (v.X > max) max = v.X;
                if (v.Y > max) max = v.Y;
                max = max / 2.3f + 120.0f;
                Vector3 target = _Position + _Direction * max;
                Matrix world = _Rotation * Matrix.CreateTranslation(target);
                _ModelRenderer.UpdateTeam(this.TeamType);
                _ModelRenderer.Draw(world, _Camera.View, _Camera.Projection);
            }
            EffectManager.Instance.GraphicsDevice.SetRenderTarget(null);
        }

        private Vector3 _Position = new Vector3(8192, 4000, 8192);
        private Vector3 _LookAt = new Vector3(8192 - 500, 0, 8192);
        private Vector3 _Up = new Vector3(0.7f, 0.6f, 0.0f);
        private Matrix _Rotation;
        private Vector3 _Direction;

        public void Startup(GameTime gameTime)
        {
            _Camera = new Camera();
            _Camera.Position = _Position;
            _Camera.LookAt = _LookAt;
            _Camera.Up = _Up;

            _Direction = _LookAt - _Position;
            _Direction.Normalize();

            //Vector3 Rotation = new Vector3(-(float)Math.PI / 2, -(float)Math.PI / 2.0f, 0);
            Vector3 Rotation = new Vector3(-(float)Math.PI / 2, -(float)Math.PI + 0.8f, -(float)Math.PI / 2);
            _Rotation = Matrix.CreateFromYawPitchRoll(Rotation.X, Rotation.Y, Rotation.Z);

            _Camera.View = Matrix.CreateLookAt(_Camera.Position, _Camera.LookAt, _Camera.Up);
            _Camera.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60), 1, 0.1f, 10000);

            _RenderTareget = new RenderTarget2D(EffectManager.Instance.GraphicsDevice, 128, 128);
        }
    }
}
